package com.example.mybatis.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatis.Pojo.Score;
import com.example.mybatis.Service.ScoreService;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "http://localhost:8081")
@RestController
public class ScoreController {

    @Autowired
    private ScoreService scoreService;


    //获取成绩信息接口
    @RequestMapping("/score")
    public String scoredata(@RequestBody Map<String, String> data) throws InterruptedException, JSONException {
        String username = data.get("username");
        String password = data.get("password");
        //通过username删除原有数据库，做更新操作
        scoreService.remove(new QueryWrapper<Score>().eq("username", username));
        //加载驱动
        System.setProperty("webdriver.chrome.driver", "D:\\a\\chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        //解决打开chrome浏览器data;,异常
        chromeOptions.addArguments("--remote-allow-origins=*");
        // 打开chrome浏览器
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.manage().window().maximize();
        // 进入石河子大学教务系统登录网址
        driver.get("http://authserver.shzu.edu.cn/authserver/login?service=https%3A%2F%2Fjwgl.shzu.edu.cn%2Fsso.jsp");
        Thread.sleep(1000);
        //输入学号，密码
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.tagName("button")).click();
        //进入成绩页面
        driver.get("https://jwgl.shzu.edu.cn/jsxsd/kscj/cjcx_list?kksj=");
        //获取table中的所有行
        List<WebElement> rows = driver.findElements(By.cssSelector("table#dataList>tbody>tr"));
        if (rows.isEmpty()) {
            System.out.println("没有获取到数据行");
        }
        // 创建JSONArray对象
        JSONArray jsonArray = new JSONArray();
        // 遍历每一行
        for (int i = 1; i < rows.size(); i++) {
            // 获取当前行的所有单元格
            List<WebElement> cells = rows.get(i).findElements(By.tagName("td"));
            // 创建JSONObject对象
            JSONObject jsonObject = new JSONObject();
            // 遍历每一个单元格
            for (int j = 0; j < cells.size(); j++) {
                // 获取单元格的文本内容
                String text = cells.get(j).getText();
                // 将单元格内容添加到JSONObject对象中
                jsonObject.put("col" + j, text);
            }
            // 将JSONObject对象添加到JSONArray对象中
            jsonArray.put(jsonObject);
        }
        //存储数据库
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Score score = new Score();
            score.setUsername(username);
            score.setId(jsonObject.getInt("col0"));
            score.setKkxq(jsonObject.getString("col1"));
            score.setKcmc(jsonObject.getString("col3"));
            score.setCj(jsonObject.getInt("col4"));
            score.setXf(jsonObject.getInt("col6"));
            score.setKcxz(jsonObject.getString("col13"));
            //存储
            scoreService.save(score);
        }

        return "success";
    }
    //根据用户名，开课学期查询成绩数据
    @ResponseBody
    @GetMapping("/scdata/{username}/{kkxq}")
    public List<Score> findByUsernameAndKxxq(@PathVariable String username, @PathVariable String kkxq) {
        return scoreService.findByUsernameAndKxxq(username, kkxq);
    }

    //用户名不为空，开课学期为空时查询成绩数据
    @ResponseBody
    @GetMapping("/scdata/{username}")
    public List<Score> findByUsernameAndKxxq1(@PathVariable String username, @RequestParam(required = false) String kkxq) {
        return scoreService.findByUsernameAndKxxq(username, kkxq);
    }

    // 按照课程性质分类并对学分进行求和
    @GetMapping("/credits/{username}")
    public ResponseEntity<Map<String, Double>> selectCreditsByNature(@PathVariable String username) {
        Map<String, Double> credits = scoreService.selectCreditsByNature(username);
        return ResponseEntity.ok(credits);
    }

    @RequestMapping("/user111")
    public List<Score> listALL(){
        List<Score> list = scoreService.list();
        return list;

    }

}



